#This program prepares the raw experiment data as a clean dataset for analysis

library(tidyverse)

rm(list=ls( )) #remove whatever was there before

setwd("...")
#cod: enter folder where the raw data is


#############SESSION 1##########################

rawsess1<-read.csv("DynCoop_4P_2022-01-25.csv", header = TRUE)
rawsess1<-rename(rawsess1, participant.id_in_session=?..participant.id_in_session  )
DCGdata_s1<-rawsess1%>%select(participant.id_in_session,participant.code,participant.label,player.id_in_group,player.contribution,player.private_acc,group.id_in_subsession,group.total_contribution,group.endowment,group.expected_group_contribution,group.treatment,group.endonext,group.repet,group.startnew,group.dropout_in_group,group.startround,group.g,group.continuation_probability, group.d,subsession.round_number,session.code)
DCGdata_s1<- rename(DCGdata_s1, DCG_P_id_in_group = player.id_in_group,DCG_P_contribution = player.contribution,,DCG_P_private_account = player.private_acc,DCG_G_id_in_subsession = group.id_in_subsession,DCG_G_total_contribution = group.total_contribution,DCG_G_endowment = group.endowment,DCG_G_threshold = group.expected_group_contribution,DCG_G_treatment = group.treatment,DCG_G_endonext = group.endonext,DCG_G_repet = group.repet,DCG_G_startnew = group.startnew,DCG_G_g=group.g,DCG_G_d=group.d,DCG_G_continuation_probability = group.continuation_probability,DCG_G_dropout_in_group = group.dropout_in_group, DCG_G_startround = group.startround,DCG_G_round = subsession.round_number )
DCGdata_s1$DCG_G_treatment=DCGdata_s1$DCG_G_treatment + 1
#Get rid of rounds that were not played (?)
DCGdata_s1<- DCGdata_s1 %>% drop_na(DCG_P_contribution)


rawsess2<-read.csv("DynCoop_1P_2022-01-25.csv", header = TRUE)
rawsess2<-rename(rawsess2, participant.id_in_session=?..participant.id_in_session  )
DCGdata_s2<-rawsess2%>%select(participant.id_in_session,participant.code,participant.label,player.id_in_group,player.contribution,player.private_acc,group.id_in_subsession,player.endowment,player.expected_group_contribution,player.treatment,player.endonext,player.repet,player.startnew, ,player.startround,player.g,player.continuation_probability,subsession.round_number,session.code)
DCGdata_s2$DCG_G_d=0.5
#DEfine player id in group and group id in a way consistent wit the 4P games:
DCGdata_s2$player.id_in_group=1
DCGdata_s2$group.id_in_subsession=DCGdata_s2$participant.id_in_session
DCGdata_s2$group.total_contribution=DCGdata_s2$player.contribution
DCGdata_s2<- rename(DCGdata_s2, DCG_P_id_in_group = player.id_in_group,DCG_P_contribution = player.contribution,DCG_P_private_account = player.private_acc,DCG_G_id_in_subsession = group.id_in_subsession,DCG_G_total_contribution = group.total_contribution,DCG_G_endowment = player.endowment,DCG_G_threshold = player.expected_group_contribution,DCG_G_treatment = player.treatment,DCG_G_endonext = player.endonext,DCG_G_repet = player.repet,DCG_G_startnew = player.startnew,DCG_G_g= player.g,DCG_G_continuation_probability = player.continuation_probability,, DCG_G_startround = player.startround,DCG_G_round = subsession.round_number )
DCGdata_s2$DCG_G_treatment=3
DCGdata_s2$DCG_G_dropout_in_group=0
#Get rid of rounds that were not played (?)
DCGdata_s2<- DCGdata_s2 %>% drop_na(DCG_P_contribution)


#merge treatments
d<- rbind(DCGdata_s1,DCGdata_s2)
rm(DCGdata_s1,DCGdata_s2)

#define session nb
slist=c("1sqq3os3","lf5oxwaa","4uvrxrtp","xltii92l","h8tcqt1o","4q1e7p3f","3gir0tim","avihsbuk","dnc1yw7p","btqzu9rq","93n9ylyd","s9naut2u","0qjnp91c","v7hxe7p4","y6a7cgw6","gok2ckuo","euw6s8ox","r8mrdrr1")
n=1
d$session_nb=0
for (s in slist){
  d$session_nb=ifelse(d$session.code == s,n,d$session_nb)
  n=n+1
}


#Generate unique participant ID
d$UniqParticipantID = d$DCG_G_treatment*10000 + d$session_nb*100 + d$participant.id_in_session
#Generate unique group ID
d$UniqGroupID = d$DCG_G_treatment*10000 + d$session_nb*100 + d$DCG_G_id_in_subsession


#risk aversion
rawriskav1<-read.csv("RiskAversion_2022-01-25.csv", header = TRUE)
rawriskav1<- rawriskav1 %>% select(participant.code,player.contribution)
rawriskav1<-rename(rawriskav1, riskaversion_contrib=player.contribution  )
d<- merge(d,rawriskav1,by="participant.code")

#raven
rawraven1<-read.csv("Raven_2022-01-25.csv", header = TRUE)
rawraven1<-aggregate(rawraven1$player.score, by = list(rawraven1$participant.code), max)
rawraven1<-rename(rawraven1, participant.code=Group.1, raven=x  )
d <- merge(d,rawraven1,by="participant.code")

#ipip
rawipip1<-read.csv("IPIP_EN_2022-01-25.csv", header = TRUE)
rawipip1<-rawipip1[rawipip1$subsession.round_number==2,]
rawipip1<- rawipip1 %>% select(participant.code,starts_with("player.ipip_score"))
names(rawipip1) <- gsub("player.ipip_score", "ipip", names(rawipip1))
d <- merge(d,rawipip1,by="participant.code")
#d<-subset(d, select=-c(DCG_G_dropout_in_group))
d<- d%>%select(-starts_with("ipip_N"))

#nep
rawnep1<-read.csv("NEP_CO2_2022-01-25.csv", header = TRUE)
rawnep1<- rawnep1 %>% select(participant.code,player.nep_score,player.co2_score)
rawnep1<-rename(rawnep1, nep=player.nep_score, co2=player.co2_score )
d <- merge(d,rawnep1,by="participant.code")

#demographic
rawdemo1<-read.csv("Demo_EN_2022-01-25.csv", header = TRUE)
rawdemo1<- rawdemo1 %>% select(participant.code,player.Gender2,player.BirthYear,player.Country2,player.Nationality,player.Education,player.Test200,player.Tongue1)
rawdemo1<-rename(rawdemo1, gender = player.Gender2,birthyear = player.BirthYear,country = player.Country2,nationality=player.Nationality,education= player.Education,dollars100=player.Test200,english_mothertongue=player.Tongue1)
d<- merge(d,rawdemo1,by="participant.code")


#Pagetimes
rawtime1<-read.csv("PageTimes-2022-01-25.csv", header = TRUE)
rawtime1<- rawtime1 %>% select(participant_code,page_index,app_name,page_name,epoch_time_completed,round_number)
rawtime1<-rename(rawtime1, participant.code=participant_code)

#remove duplicates *sort in descending times, then remove duplicates
rawtime1<- rawtime1[order(rawtime1$epoch_time_completed, decreasing=TRUE),]
rawtime1<- distinct(rawtime1, participant.code, page_index, .keep_all = TRUE)

rawtime1<- rawtime1[order(rawtime1$participant.code, rawtime1$page_index  ),]
#options(warn = 2)   
  for (i in unique(rawtime1$participant.code)){ prevn=0
    for (n in rawtime1$page_index[rawtime1$participant.code==i]){
      rawtime1$timeonpage[rawtime1$page_index==n & rawtime1$participant.code==i] <-  rawtime1$epoch_time_completed[rawtime1$page_index==n & rawtime1$participant.code==i] - rawtime1$epoch_time_completed[rawtime1$page_index==prevn & rawtime1$participant.code==i]
      prevn=n
      }
  }

aggtime1 <- aggregate(rawtime1$timeonpage, by=list(rawtime1$participant.code,rawtime1$app_name), sum)
aggtime1 <- rename(aggtime1, participant.code=Group.1, app_name=Group.2, timeonpage=x)
#aggtime1<- aggtime1[aggtime1$app_name != "Welcome",]
aggtime1<-aggtime1[aggtime1$timeonpage>0,]
aggtime1<- reshape(aggtime1, timevar="app_name", idvar=c("participant.code"), direction="wide")
aggtime1 <- rename(aggtime1, time_welcome=timeonpage.Welcome,time_demo=timeonpage.Demo_EN, time_DCG1=timeonpage.DynCoop_1P, time_DCG4=timeonpage.DynCoop_4P, time_instr4=timeonpage.EN_Instr_Compr,time_instr1=timeonpage.EN_Instr_Compr_1P, time_ipip=timeonpage.IPIP_EN, time_raven=timeonpage.Raven, time_risk=timeonpage.RiskAversion)
dat$e <- rowSums(dat[,c("b", "c")], na.rm=TRUE)
aggtime1$time_DCG=rowSums(aggtime1[,c("time_DCG1", "time_DCG4")], na.rm=TRUE)
aggtime1$time_instr=rowSums(aggtime1[,c("time_instr1", "time_instr4")], na.rm=TRUE)

aggtime1<-subset(aggtime1, select=-c(time_DCG1, time_DCG4, time_instr1, time_instr4))
aggtime1$time_total <- rowSums(aggtime1[2:9])
  
  #aggtime1$time_demo + aggtime1$time_DCG + aggtime1$time_instr + aggtime1$time_ipip+aggtime1$time_raven + aggtime1$time_risk

d <- merge(d,aggtime1,by="participant.code")

#R> subset(df, select=-c(z,u))


#merge 1p and 4p time on page
#aggtime1$timeonpage.DynCoop <- ifelse(d$DCG_G_treatment<3,aggtime1$timeonpage.DynCoop_4P,aggtime1$timeonpage.DynCoop_1P)


#Remove things we do not need:
#rm(rawdemo1,rawipip1,rawraven1,rawriskav1,rawsess1,rawsess2,rawnep1)



#save(alldata_s1, file="data1")

save.image("fulldata.RData")



